package com.springboot.prac.proxy_02;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class ProxyTest {
    public static void main(String[] args) {
        Person zhangsan = new Student("张三");

        InvocationHandler stuHandler = new StuInvocationHandler<Person>(zhangsan);
        //加入下面代码以输出类到文件
        Person stuProxy = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class<?>[]{Person.class}, stuHandler);
        stuProxy.giveMoney();

        byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0", zhangsan.getClass().getInterfaces());
        String path = "D:\\project\\code\\java\\prac\\target\\classes\\com\\springboot\\prac\\proxy_02\\stuproxy.class";
        try (FileOutputStream fos = new FileOutputStream(path)) {
            fos.write(classFile);
            fos.flush();
            System.out.println("代理类class文件写入成功");
        } catch (Exception e) {
            System.out.println("写文件错误");
        }
    }
}
